package com.note.servlet.user;

import com.note.pojo.Users;
import com.note.service.user.userService;
import com.note.service.user.userServiceImpl;
import com.note.utils.Constants;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class updateName extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nickname = req.getParameter("nickname");
        userService u = new userServiceImpl();
        Users userSession = (Users) req.getSession().getAttribute("userSession");
        int id = (int) userSession.getId();
        boolean b = u.updateName(nickname, id);
        if (b){
            userSession.setNickname(nickname);
            req.getSession().setAttribute(Constants.USER_SESSION, userSession);
            resp.sendRedirect(req.getContextPath()+"/jsp/myInfo.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
